import { ActionTypes } from "..";
import {menu} from '@/pages/layout/menu';

// 路由跳转检测
export const ROUTERCHANGE = (state:StateProps,action:ActionTypes)=>{
  let isExit = false;
  
  state.routerStack.forEach(item=>{
    if(item.path == action.payload){
      isExit = true;
    }
  })
  // 从menu中根据地址获取路由
  if(!isExit){
    menu.forEach(item=>{
      if(item.children){
        item.children.forEach(child=>{
          if(child.path == action.payload){
            state.routerStack.push({
              title:child.title,
              path:child.path
            })
            if(state.routerStack.length > 10){
              state.routerStack.splice(0,1);
            }
          }
        })
      }else if(item.path == action.payload){
        state.routerStack.push({
          path:action.payload,
          title:item.title,
        })
      }
    })
  }
  state.routerPath = action.payload;
}
// 关闭某个栈
export const CLOSE_ROUTERSTACK = (state:StateProps,action:ActionTypes)=>{
  let index;
  state.routerStack.forEach((item,i)=>{
    if(item.path == action.payload){
      index = i;
    }
  })
  if(index || index == 0){
    state.routerStack.splice(index,1);
  };
}